﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using DataAccessLayer.EntityDataModel;
using Dev_house_Project.DataAccessLayer.EntityDataModel;

namespace Dev_house_Project.DataService.DataProviders
{
    internal class CompanyDataProvider
    {
        private readonly EntityDataModel _context;

        public CompanyDataProvider(string connectionString)
        {
            _context = new EntityDataModel(connectionString);
        }

        public List<Company> GetAllCompanies()
        {
            IQueryable<Company> list = from company in _context.CompanySet
                                       select company;
            return list.ToList();            
        }

        public void UpdateCompanies(List<Company> companies)
        {
            foreach (Company company in companies)
            {
                _context.Attach(company);
                company.SetAllModified(_context); // custom extension method

                try
                {
                    _context.SaveChanges();
                }
                catch (OptimisticConcurrencyException e)
                {
                    throw (e);
                }
            }
        }
    }
}